package com.tanhua.server.controller;

import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.PhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/users/phone")
public class PhoneController {

    @Autowired
    private PhoneService phoneService;

    @PostMapping("/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){

        String phone = UserHolder.getMobile();
        phoneService.sendMsg(phone);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){

        String code = (String) map.get("verificationCode");
        boolean verification = phoneService.checkVerificationCode(code);
        return ResponseEntity.ok(verification);
    }

    @PostMapping
    public ResponseEntity save(@RequestBody Map map){

        String phone = (String) map.get("phone");
        phoneService.update(phone);
        return ResponseEntity.ok(null);
    }
}
